package server;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;

public class Bootstrap {
    //定义socket监听的端口号
    private int port =8080;
    private String webappsPath ="";

    //private Map<String,HttpServlet> servletMap = new HashMap<String,HttpServlet>();

    private Mapper mapper =new Mapper();

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getWebappsPath() {
        return webappsPath;
    }

    public void setWebappsPath(String webappsPath) {
        this.webappsPath = webappsPath;
    }

    public void start() throws Exception {
        //加载解析配置server.xml，
        loadServer();

        //加载解析相关的配置，web.xml
        //loadServlet();

        //定义一个线程池
        int corePoolSize =10;
        int maximumPoolSize =50;
        long keepAliveTime =100L;
        TimeUnit unit =TimeUnit.SECONDS;
        BlockingQueue<Runnable> workQueue =new ArrayBlockingQueue<>(50);
        ThreadFactory threadFactory =Executors.defaultThreadFactory();
        RejectedExecutionHandler handler =new ThreadPoolExecutor.AbortPolicy();

        ThreadPoolExecutor threadPoolExecutor =new ThreadPoolExecutor(
                corePoolSize,
                maximumPoolSize,
                keepAliveTime,
                unit,
                workQueue,
                threadFactory,
                handler
        );

        /*
        * 完成Minicat 1.0版本
        *  需求：浏览器请求http://localhost:8080,返回一个固定的字符串到页面“Hello Minicat!”
        * */
        ServerSocket serverSocket = new ServerSocket(port);
        System.out.println("====>Minicat start on port:"+port);

        /*while (true){
            Socket socket = serverSocket.accept();
            //有了socket,接收到请求，获取输出流
            OutputStream outputStream = socket.getOutputStream();
            String data ="Hello Minicat!";
            String responseText = HttpProtocolUtil.getHttpHeader200(data.getBytes().length) + data;
            outputStream.write(responseText.getBytes());
            socket.close();
        }*/

        /*
         * 完成Minicat 2.0版本
         * 封装Request和Response对象，返回html静态资源文件
         */
//        while(true){
//            Socket socket = serverSocket.accept();
//            InputStream inputStream = socket.getInputStream();
//            //封装Request和Response对象，返回html静态资源文件
//            Request request = new Request(inputStream);
//            Response response = new Response(socket.getOutputStream());
//
//            response.outPutHtml(request.getUrl());
//            socket.close();
//        }

        /*
         * 完成Minicat 3.0版本
         * 需求： 可以请求动态资源（Servlet）
         */
//        while(true){
//            Socket socket = serverSocket.accept();
//            InputStream inputStream = socket.getInputStream();
//            //封装Request和Response对象，返回html静态资源文件
//            Request request = new Request(inputStream);
//            Response response = new Response(socket.getOutputStream());
//
//            //静态资源处理
//            if (servletMap.get(request.getUrl()) ==null){
//                response.outPutHtml(request.getUrl());
//            }else{
//                //动态资源servlet请求
//                HttpServlet httpServlet =servletMap.get(request.getUrl());
//                httpServlet.service(request,response);
//            }
//
//            socket.close();
//        }

        /*
        * 多线程改造 (不用线程池)
        */
//        while(true){
//            Socket socket = serverSocket.accept();
//            RequestProcessor requestProcessor = new RequestProcessor(socket, servletMap);
//            requestProcessor.start();
//        }

        System.out.println("=====>>> 使用线程池进行多线程改造");

        /*
        * 多线程改造 （用线程池）
        */
//        while(true){
//            Socket socket = serverSocket.accept();
//            RequestProcessor requestProcessor = new RequestProcessor(socket, servletMap);
//            //requestProcessor.start();
//            threadPoolExecutor.execute(requestProcessor);
//        }

        while(true){
            Socket socket = serverSocket.accept();
            RequestProcessor requestProcessor = new RequestProcessor(socket, mapper,webappsPath);
            //requestProcessor.start();
            threadPoolExecutor.execute(requestProcessor);
        }

    }

    //加载解析web.xml,初始化servlet
//    private void loadServlet() {
//        InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("web.xml");
//        SAXReader saxReader = new SAXReader();
//
//        try {
//            Document document = saxReader.read(resourceAsStream);
//            Element rootElement = document.getRootElement();
//            List<Element> selectNodes = rootElement.selectNodes("//servlet");
//            for (int i =0;i <selectNodes.size();i++){
//                Element element = selectNodes.get(i);
//                //<servlet-name>lagou</servlet-name>
//                Element servletnameElement = (Element)element.selectSingleNode("servlet-name");
//                String servletName = servletnameElement.getStringValue();
//                //<servlet-class>server.LagouServlet</servlet-class>
//                Element servletclassElement = (Element)element.selectSingleNode("servlet-class");
//                String servletClass = servletclassElement.getStringValue();
//
//                //根据servlet-name的值找到url-pattern
//                Element servletMapping = (Element)rootElement.selectSingleNode("/web-app/servlet-mapping[servlet-name='" + servletName + "']");
//                //  /lagou
//                String urlPattern = servletMapping.selectSingleNode("url-pattern").getStringValue();
//
//                servletMap.put(urlPattern, (HttpServlet) Class.forName(servletClass).newInstance());
//            }
//
//
//        } catch (DocumentException e) {
//            e.printStackTrace();
//        } catch (ClassNotFoundException e) {
//            e.printStackTrace();
//        } catch (IllegalAccessException e) {
//            e.printStackTrace();
//        } catch (InstantiationException e) {
//            e.printStackTrace();
//        }
//    }

    private void loadServer(){
        InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("server.xml");
        SAXReader saxReader = new SAXReader();

        try{
            Document document = saxReader.read(resourceAsStream);
            Element rootElement = document.getRootElement();
            List<Element> selectNodes = rootElement.selectNodes("//Service");
            for (int i =0;i <selectNodes.size();i++){
                Element element = selectNodes.get(i);
                //<Connector port="8080"/>
                Element connectorElement = (Element)element.selectSingleNode("Connector");
                String portStr =connectorElement.attributeValue("port");
                //设置监听端口号
                setPort(Integer.parseInt(portStr));

                List<Element> engineNodes = element.selectNodes("//Engine");
                for (int j =0;j <engineNodes.size();j++){
                    Element engineElement = engineNodes.get(j);
                    //<Host name="localhost" appBase="E:\idea_workspace_02\webapps"></Host>
                    Element hostElement = (Element)engineElement.selectSingleNode("Host");
                    String hostName = hostElement.attributeValue("name"); //localhost
                    String appBase = hostElement.attributeValue("appBase");//webapps 绝对路径
                    //设置webappsPath
                    setWebappsPath(appBase);

                    Host host =new Host();
                    mapper.getHostMap().put(hostName,host);

                    File file = new File(appBase);
                    String[] contextArr = file.list(); //demo1,demo2
                    for (int h =0;h <contextArr.length;h++){
                        String contextName =contextArr[h];
                        String webXmlPath =appBase +"\\"+contextName+"\\web.xml";

                        Context context = new Context();
                        host.getContextMap().put(contextName,context);

                        loadContext(new FileInputStream(webXmlPath),context);
                    }
                }

                //System.out.println(host);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void loadContext(InputStream resourceAsStream,Context context) {
        //InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("web.xml");
        SAXReader saxReader = new SAXReader();

        try {
            Document document = saxReader.read(resourceAsStream);
            Element rootElement = document.getRootElement();
            List<Element> selectNodes = rootElement.selectNodes("//servlet");
            Map<String,HttpServlet> servletMap = new HashMap<String,HttpServlet>();

            for (int i =0;i <selectNodes.size();i++){
                Element element = selectNodes.get(i);
                //<servlet-name>lagou</servlet-name>
                Element servletnameElement = (Element)element.selectSingleNode("servlet-name");
                String servletName = servletnameElement.getStringValue();
                //<servlet-class>server.LagouServlet</servlet-class>
                Element servletclassElement = (Element)element.selectSingleNode("servlet-class");
                String servletClass = servletclassElement.getStringValue();

                //根据servlet-name的值找到url-pattern
                Element servletMapping = (Element)rootElement.selectSingleNode("/web-app/servlet-mapping[servlet-name='" + servletName + "']");
                //  /lagou
                String urlPattern = servletMapping.selectSingleNode("url-pattern").getStringValue();

                servletMap.put(urlPattern, (HttpServlet) Class.forName(servletClass).newInstance());// /lagou,instance
            }
            //往容器设置值
            Wrapper wrapper = new Wrapper(servletMap);
            context.setWrapper(wrapper);

        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
    }

    /**
     * Minicat的程序启动入口
     *
     */
    public static void main(String[] args) {
        Bootstrap bootstrap = new Bootstrap();
        try {
            bootstrap.start();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
